#include <head.h>
void ls_wc()
{
	int pipefd[2];
	pid_t cpid;
	//char buf;
	if (pipe(pipefd) == -1) {
		perror("pipe");
		exit(EXIT_FAILURE);
	}

	cpid = fork();
	if (cpid == -1) {
		perror("fork");
		exit(EXIT_FAILURE);
	}
	if (cpid > 0) {    /* Child reads from pipe */
		close(pipefd[1]);          /* Close unused write end */
		// read(pipefd[0], buf, 1024);
		int r =dup2(pipefd[0],STDIN_FILENO);
		if(r<0){
			perror("dup2 error");
			exit(1);
		}
		
		execlp("wc","wc","-l",NULL);

		write(STDOUT_FILENO, "\n", 1);
		close(pipefd[0]);
		_exit(EXIT_SUCCESS);

	} else {            /* Parent writes argv[1] to pipe */
		close(pipefd[0]);          /* Close unused read end */
		int r =dup2(pipefd[1],STDOUT_FILENO);

		if(r<0){
			perror("dup2 error");
			exit(1);
		}
		execlp("ls","ls",NULL);

		close(pipefd[1]);          /* Reader will see EOF */
		wait(NULL);                /* Wait for child */
		exit(EXIT_SUCCESS);
	}


}
